/*  
 * @(#) Constants.java Create on 2014-03-12 下午04:03:50   
 *   
 * Copyright 2014 by fibrlink.   
 */

package com.springdemo.module.systemmanager.service;

import com.springdemo.basic.common.UserContantData;
import com.springdemo.basic.global.page.BasePage;
import com.springdemo.basic.global.service.BaseService;
import com.springdemo.module.systemmanager.dao.UserDao;
import com.springdemo.module.systemmanager.entity.myentity.MyUser;
import com.springdemo.tools.ImageHelper;
import com.springdemo.tools.StringHelper;
import com.springdemo.tools.security.AESCryptos;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 
 * @author san 
 * @date   2014-03-12
 */
@Service
public class UserService<T> extends BaseService<T> {

    @Resource
    private UserDao<T> userDao;


    public UserDao<T> getDao() {
        return userDao;
    }



    /**
     * 查询用户列表
     *
     * @throws Exception
     * @Title: queryUserByList
     * @data: 2014-03-12 下午04:51:50
     * @author: shuqi
     */
    public List<MyUser> queryUserByList( BasePage page ) throws Exception {
        page.getPager().setColum("t.org");
        Integer rowCount = userDao.queryUserByCount(page);
        page.getPager().setRowCount(rowCount);
        page.setSort("t.create_time");
        page.setOrder("desc,t.id asc");
        return userDao.queryUserByList(page);
    }

    /**
     * 根据id查询用户信息
     *
     * @throws Exception
     * @Title: queryUserById
     * @data: 2014-03-12 下午04:51:50
     * @author: shuqi
     */
    public MyUser queryUserById( Object id ) throws Exception {
        return getDao().queryUserById(id);
    }


    /**
     * 保存头像图片
     *
     * @param x
     * @param y
     * @param w
     * @param h
     * @param oldPath
     * @return
     * @throws Exception
     * @Title: saveHeadPicture
     * @data:2014-3-21上午09:53:19
     * @author:san
     */
    public String saveHeadPicture( double x, double y, double w, double h, String oldPath, String projectPath, String userid, String savePath ) {
        String result = "1";
        if (!StringHelper.toTrim(oldPath).equals("") && !StringHelper.toTrim(userid).equals("") && w > 0 && !StringHelper.toTrim(savePath).equals("")) {
            String outputPath = projectPath + savePath;
            try {
                int xx = (int) (x);
                int yy = (int) (y);
                int ww = (int) (w);
                int hh = (int) (h);
                //生成裁剪后的图片
                ImageHelper img = ImageHelper.load(projectPath + oldPath);
                img.zoomTo(UserContantData.USER_HEAD_PHOTO_PICTURE_WIDTH, UserContantData.USER_HEAD_PHOTO_PICTURE_HEIGHT);
                img.cutTo(xx, yy, ww, hh);
                img.save(outputPath, "jpg");
                Map<String, String> map = new HashMap<String, String>();
                map.put("picture", savePath);
                map.put("id", userid);
                this.getDao().updateHeadPhoto(map);
            } catch (Exception e) {
                e.printStackTrace();
                result = "0";
            }

        } else {
            result = "-3";
        }
        return result;
    }

    /**
     * 上传正式照片
     *
     * @param userid
     * @param savePath
     * @return
     * @Title: saveRealyHeadPicture
     * @data:2014-7-17下午02:17:10
     * @author:san
     */
    public String saveRealyHeadPicture( String userid, String savePath ) {
        String result = "1";
        if (!StringHelper.toTrim(savePath).equals("")) {
            try {

                Map<String, String> map = new HashMap<String, String>();
                map.put("picture", savePath);
                map.put("id", userid);
                this.getDao().updatePhoto(map);
            } catch (Exception e) {
                e.printStackTrace();
                result = "0";
            }

        } else {
            result = "-3";
        }
        return result;
    }


    /**
     * 查询用户信息是否重复
     *
     * @throws Exception
     * @Title: noRepeat
     * @data: 2014-03-12 下午04:03:50
     * @author: shuqi
     */
    public Integer noRepeat( MyUser myUserPage ) throws Exception {
        if (!StringHelper.toTrim(myUserPage.getRegname()).equals("")) {
            //加密
            String regname = AESCryptos.aesEncrypt(myUserPage.getRegname(), AESCryptos.SAVE_KEY);
            myUserPage.setRegname(regname);
        }

        if (!StringHelper.toTrim(myUserPage.getEmail()).equals("")) {
            //加密
            String email = AESCryptos.aesEncrypt(myUserPage.getEmail(), AESCryptos.SAVE_KEY);
            myUserPage.setEmail(email);
        }
        return this.userDao.noRepeat(myUserPage);
    }


    /**
     * 获取通知已选人员数据
     *
     * @Title: noticeUserList
     * @data:2015-8-31下午02:47:45
     * @author:ChengQ
     */
    public List<MyUser> noticeUserList( MyUser noticeUser ) throws Exception {
        Integer rowCount = this.userDao.queryNoticeUserByCount(noticeUser);
        noticeUser.getPager().setRowCount(rowCount);
        return userDao.noticeUserList(noticeUser);
    }


}